package com.yidi.system.repository;

import com.yidi.common.core.Repository.BaseRepository;
import com.yidi.system.domain.SysUserRole;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author ：Xn
 * @ClassName: SysUserRoleRepository
 * @date ：2021-10-19 14:09
 * @describe ：用户和角色
 */
@Repository
public interface SysUserRoleRepository extends BaseRepository<SysUserRole,Long> {

    /**
     * 通过角色ID查询角色使用数量
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Query(nativeQuery = true, value = "select count(1) from sys_user_role where role_id = ?1")
    Integer countUserRoleByRoleId(Long roleId);

    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "delete from sys_user_role where user_id=?1 and role_id=?2")
    void deleteUserRoleInfo(Long userId, Long roleId);

    /**
     * 通过用户ID删除用户和角色关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "delete from sys_user_role where user_id = ?1")
    void deleteUserRoleByUserId(Long userId);

    List<SysUserRole> findByUserId(Long userId);
}
